package com.itcast.druid;

import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

import javax.sql.DataSource;

import org.junit.Test;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.itcast.dbutil.DBUtils;

public class DruidDemo {
	public static DataSource getDataSource() throws Exception {
		Properties p = new Properties();
		p.load(new FileInputStream("src/druid.properties"));
		//获取druid的连接池
		DataSource dataSource = DruidDataSourceFactory
				.createDataSource(p);
		return dataSource;
	}
	
	@Test
	public void test(){
		Connection con = null;
		PreparedStatement statement = null;
		ResultSet rs = null;
		try {
			DataSource dataSource = DruidDemo.getDataSource();
			con = dataSource.getConnection();
			statement = con.prepareStatement("select * from salaries limit 10");
			rs = statement.executeQuery();
			while(rs.next()){
				System.out.println(rs.getInt(1)+":"+rs.getInt(2)+":"+rs.getDate(3)+":"+rs.getDate(4));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			DBUtils.release(con, statement, rs);
		}
	}
}
